package com.google.code.sysarch.v4.graphical;

import org.lwjgl.opengl.GL11;

public class Button {
	
	private Texture[] texture;
	private int textureIndex;
	private int x, y;
	private int width, height;
	private int name;
	
	Button(String filename, int x, int y, int name) {
		this.texture = new Texture[1];
		this.texture[0] = TextureLoader.get().getTexture(filename);
		this.textureIndex = 0;
		this.x = x;
		this.y = y;
		this.width = texture[0].getImageWidth();
		this.height = texture[0].getImageHeight();
		this.name = name;		
	}
	
	Button(String[] filename, int x, int y, int name) {
		this.texture = new Texture[filename.length];
		for (int i=0; i<filename.length; i++) {
			this.texture[i] = TextureLoader.get().getTexture(filename[i]);
		}
		this.textureIndex = 0;
		this.x = x;
		this.y = y;
		this.width = texture[0].getImageWidth();
		this.height = texture[0].getImageHeight();
		this.name = name;
	}
	
	public void setTexture(int index) {
		this.textureIndex = index;
	}
	
	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public void setName(int name) {
		this.name = name;
	}
	
	public int getName() {
		return this.name;
	}
	
	public void setDimension(int width, int height) {
		this.width = width;
		this.height = height;
	}
	
	public void render() {
		float x = (float) this.x;
		float y = (float) this.y;
		float w = (float) this.width;
		float h = (float) this.height;
		texture[textureIndex].bind();
		GL11.glColor3f(1, 1, 1);
		GL11.glLoadName(this.name);
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0, 0);
			GL11.glVertex2f(x, y);
			GL11.glTexCoord2f(0, 1);
			GL11.glVertex2f(x, y+h);
			GL11.glTexCoord2f(1, 1);
			GL11.glVertex2f(x+w, y+h);
			GL11.glTexCoord2f(1, 0);
			GL11.glVertex2f(x+w, y);
		GL11.glEnd();
	}
	
	public boolean isMouseOver(int mx, int my) {
		if (mx >= this.x && mx < this.x + this.width && my >= this.y && my < this.y + this.height) {
			return true;
		} else {
			return false;
		}
	}
	
}
